﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    internal class GradientPicker : EditorWindow
    {
        private const int k_DefaultNumSteps = 0;
        private GUIView m_DelegateView;
        private Gradient m_Gradient;
        private GradientEditor m_GradientEditor;
        private static GradientPicker s_GradientPicker;

        private GradientPicker()
        {
            base.hideFlags = HideFlags.DontSave;
        }

        private void Init(Gradient newGradient, GUIView currentView)
        {
            this.m_Gradient = newGradient;
            this.m_DelegateView = currentView;
            if (this.m_GradientEditor != null)
            {
                this.m_GradientEditor.Init(newGradient, 0);
            }
        }

        public void OnDisable()
        {
            if (this.m_GradientEditor != null)
            {
                this.m_GradientEditor.Clear();
            }
            s_GradientPicker = null;
        }

        public void OnEnable()
        {
        }

        public void OnGUI()
        {
            if (this.m_Gradient != null)
            {
                if (this.m_GradientEditor == null)
                {
                    this.m_GradientEditor = new GradientEditor();
                    this.m_GradientEditor.Init(this.m_Gradient, 0);
                }
                Rect position = new Rect(10f, 10f, base.position.width - 20f, base.position.height - 20f);
                this.m_GradientEditor.OnGUI(position);
                if (GUI.changed)
                {
                    this.SendEvent(true);
                }
            }
        }

        private void OnPlayModeStateChanged()
        {
            base.Close();
        }

        private void SendEvent(bool exitGUI)
        {
            if (this.m_DelegateView != null)
            {
                Event e = EditorGUIUtility.CommandEvent("GradientPickerChanged");
                base.Repaint();
                this.m_DelegateView.SendEvent(e);
                if (exitGUI)
                {
                    GUIUtility.ExitGUI();
                }
            }
        }

        public static void Show(Gradient newGradient)
        {
            GUIView current = GUIView.current;
            if (s_GradientPicker == null)
            {
                s_GradientPicker = (GradientPicker) EditorWindow.GetWindow(typeof(GradientPicker), true, "Gradient Editor", false);
                Vector2 vector = new Vector2(360f, 112f);
                Vector2 vector2 = new Vector2(1900f, 150f);
                s_GradientPicker.minSize = vector;
                s_GradientPicker.maxSize = vector2;
                s_GradientPicker.ShowAuxWindow();
            }
            else
            {
                s_GradientPicker.Repaint();
            }
            s_GradientPicker.Init(newGradient, current);
        }

        public static Gradient gradient
        {
            get
            {
                if (s_GradientPicker != null)
                {
                    return s_GradientPicker.m_Gradient;
                }
                return null;
            }
        }

        public static bool visible
        {
            get
            {
                return (s_GradientPicker != null);
            }
        }
    }
}

